package edu.pku.dao;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import edu.pku.data.Product;
import edu.pku.util.MySqlMapClient;

public class ProductDao {
	
	public void addProduct(Product p) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		sqlMap.insert("insert_product",p);
	}
	
	public boolean isNameExist(String companyName,String name) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		List<String> list = sqlMap.queryForList("get_product_name",companyName);
		if(list.contains(name))
			return true;
		return false;
	}
	
	public List<Product> getProduct(String companyName) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		List<Product> list =  sqlMap.queryForList("get_product",companyName);
		return list;
	}
	
	public void deleteProduct(Product p) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		sqlMap.delete("delete_product",p);
	}
	
	public void updateProduct(Product p) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		sqlMap.update("update_product",p);
	}
}
